package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.enums.ImgRelationEnum;
import com.ruoyi.system.domain.enums.ImgTypeEnum;
import com.ruoyi.system.domain.equipment.*;
import com.ruoyi.system.mapper.equipment.BxwxImgMapper;
import com.ruoyi.system.mapper.equipment.EquipmentBookMapper;
import com.ruoyi.system.mapper.equipment.EquipmentBxwxMapper;
import com.ruoyi.system.mapper.equipment.EquipmentFaultTypeMapper;
import com.ruoyi.system.service.IEquipmentBxwxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备报修维修Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-18
 */
@Service
@RequiredArgsConstructor
public class EquipmentBxwxServiceImpl extends ServiceImpl<EquipmentBxwxMapper, EquipmentBxwx> implements IEquipmentBxwxService {

    private final BxwxImgMapper bxwxImgMapper;
    private final EquipmentFaultTypeMapper equipmentFaultTypeMapper;
    private final EquipmentBxwxMapper equipmentBxwxMapper;
    private final EquipmentBookMapper equipmentBookMapper;

    @Override
    public int add(EquipmentBxwx equipmentBxwx) {
      //  生成保修单号
        String bxNo=genBxNo();
        equipmentBxwx.setBxNumber(bxNo);
        //根据设备id获取设备名称和类型
        BxwxDTO dto=equipmentBookMapper.selectEquipmentBookById(equipmentBxwx.getEquipmentId());
        equipmentBxwx.setEquipmentCode(dto.getEquipmentCode());
        equipmentBxwx.setEquipmentId(dto.getEquipmentId());
        equipmentBxwx.setEquipmentName(dto.getEquipmentName());
        equipmentBxwx.setEquipmentType(dto.getEquipmentType());
        equipmentBxwx.setEquipmentTypeId(dto.getEquipmentTypeId());
        equipmentBxwx.setEquipmentWz(dto.getEquipmentWz());
        baseMapper.insert(equipmentBxwx);
      if (equipmentBxwx.getImgPath()!=null&&equipmentBxwx.getImgPath().size()>0) {
          for (String path : equipmentBxwx.getImgPath()) {
              BxwxImg img = new BxwxImg();
              img.setImgPath(path);
              img.setRelationId(equipmentBxwx.getWxId());
              img.setRelationType(ImgRelationEnum.REPAIR.getValue());
              img.setType(ImgTypeEnum.BX.getValue());
              bxwxImgMapper.insert(img);
          }
      }
      return 1;
    }

    /**
     * 生成报修单号
     * @return
     */
    private synchronized String genBxNo() {
        String start="BX";
        String mid = DateUtil.format(new Date(), "yyyyMMdd");
        String maxBxNo=equipmentBxwxMapper.selectMaxBxNo(start+mid+"%");
        if (maxBxNo!=null){
            int num = new Integer(maxBxNo.substring(10)) + 1;
            return start+mid+String.format("%04d",num);
        }
        return start+mid+"0001";
    }
    private synchronized String genBxNo2(){
        String start="BX";
        String mid = DateUtil.format(new Date(), "yyyyMMdd");
        String maxBxNo=equipmentBxwxMapper.selectMaxBxNo2();
        if (maxBxNo!=null){
            if ((start+mid).equals(maxBxNo.substring(0,10))){
                int num = new Integer(maxBxNo.substring(10)) + 1;
                return start+mid+String.format("%04d",num);
            }
        }
        return start+mid+"0001";
    }

    @Override
    public int executeRepair(EquipmentBxwx equipmentBxwx) {
    //    修改成已维修
        baseMapper.update(null, Wrappers.<EquipmentBxwx>lambdaUpdate()
                .set(EquipmentBxwx::getRepairTime,equipmentBxwx.getRepairTime())
                .set(EquipmentBxwx::getServiceContent,equipmentBxwx.getServiceContent())
                .set(EquipmentBxwx::getStatus,equipmentBxwx.getStatus())
                .set(EquipmentBxwx::getUpdateBy, SecurityUtils.getUsername())
                .eq(EquipmentBxwx::getWxId,equipmentBxwx.getWxId()));
        //保存上传的图片
        if (equipmentBxwx.getImgPath()!=null&&equipmentBxwx.getImgPath().size()>0) {
            for (String path : equipmentBxwx.getImgPath()) {
                BxwxImg img = new BxwxImg();
                img.setImgPath(path);
                img.setRelationId(equipmentBxwx.getWxId());
                img.setRelationType(ImgRelationEnum.REPAIR.getValue());
                img.setType(ImgTypeEnum.WX.getValue());
                bxwxImgMapper.insert(img);
            }
        }
        //    将内容添加到知识库
        EquipmentBxwx one = baseMapper.selectOne(Wrappers.<EquipmentBxwx>lambdaQuery()
                .eq(EquipmentBxwx::getWxId, equipmentBxwx.getWxId()));

        EquipmentFaultType equipmentFaultType = new EquipmentFaultType();
        equipmentFaultType.setFaultInfo(one.getFaultDescribe());
        equipmentFaultType.setFaultType(one.getFaultType());
        equipmentFaultType.setEquipmentType(one.getEquipmentType());
        equipmentFaultType.setEquipmentCode(one.getEquipmentCode());
        equipmentFaultType.setEquipmentName(one.getEquipmentName());
        equipmentFaultType.setEquipmentId(one.getEquipmentId());
        equipmentFaultType.setResolveMethod(equipmentBxwx.getServiceContent());
        equipmentFaultType.setCreateBy(SecurityUtils.getUsername());
        equipmentFaultType.setCreateTime(new Date());
        equipmentFaultTypeMapper.insert(equipmentFaultType);
        //绑定知识库的图片
        List<BxwxImg> bxwxImgList = bxwxImgMapper.selectList(Wrappers.<BxwxImg>lambdaQuery()
                .eq(BxwxImg::getRelationId, equipmentBxwx.getWxId())
                .eq(BxwxImg::getRelationType, ImgRelationEnum.REPAIR.getValue()));
        if (CollectionUtil.isNotEmpty(bxwxImgList)) {
            for (BxwxImg bxwxImg : bxwxImgList) {
                bxwxImg.setId(null);
                bxwxImg.setRelationType(ImgRelationEnum.KNOWLEDGE.getValue());
                bxwxImg.setRelationId(equipmentFaultType.getFaultTypeId());
                bxwxImgMapper.insert(bxwxImg);
            }
        }
        return 1;
    }

    @Override
    public EquipmentBxwx getByIdWithImg(Long wxId) {
        EquipmentBxwx equipmentBxwx = baseMapper.selectById(wxId);
        List<BxwxImg> bxwxImgList = bxwxImgMapper.selectList(Wrappers.<BxwxImg>lambdaQuery()
                .select(BxwxImg::getImgPath)
                .eq(BxwxImg::getRelationId, wxId)
                .eq(BxwxImg::getRelationType, ImgRelationEnum.REPAIR.getValue()));
        List<String> imgPath = bxwxImgList.stream().map(BxwxImg::getImgPath).collect(Collectors.toList());
        equipmentBxwx.setImgPath(imgPath);
        return equipmentBxwx;
    }
}
